package com.mrd.util;

import java.util.Map;

/**
 * <p> application name: sample <p> copyright: Copyright@2012 明睿达科技有限公司
 */

/**
 * @author winaya
 */
public class DownloadPropertiesUtil {
    // 外网ip和端口
    private static String netAddress = "";

    private static final String ip = "ip";

    private static final String port = "port";

    private static final String contextPath = "contextPath";

    private static final String downloadProperties = "download.properties";
    // 下载地址前缀
    private static String url = "";

    /**
     * 获取外网的ip和端口 返回"http://ip:端口/"的形式
     */
    private static String getNetAddress() {
        if (org.apache.commons.lang3.StringUtils.isEmpty(netAddress)) {
            String file = FileUtil.getPropertiesPath(downloadProperties);
            Map<String, String> properties = FileUtil.readIni(file);
            String ip = properties.get(DownloadPropertiesUtil.ip);
            String port = properties.get(DownloadPropertiesUtil.port);
            String contextPath = properties.get(DownloadPropertiesUtil.contextPath);
            if (org.apache.commons.lang3.StringUtils.isNotEmpty(ip) && org.apache.commons.lang3.StringUtils.isNotEmpty(port)) {
                netAddress = "http://" + ip + ":" + port + "/";
                if (org.apache.commons.lang3.StringUtils.isNotEmpty(contextPath)) {
                    netAddress += contextPath + "/";
                }
            }
        }
        return netAddress;
    }

    /**
     * 获取下载地址
     */
    public static String getDlNetAddress(String attachmentId) {
        String netDress = getNetAddress();
        // 如果不能拿到外网地址，则返回空下载
        if (org.apache.commons.lang3.StringUtils.isNotEmpty(netDress)) {
            // 如果url未组合，则将其进行组合
            if (org.apache.commons.lang3.StringUtils.isEmpty(url)) {
                url = netAddress + "business/download.do?attachment.id=";
            }
            return url + attachmentId;
        }
        return "";
    }
}
